<?php

class Parser {

    public $siteUrl;
    private $name_start;    //название товара
    private $name_end;
    private $art_start;     //артикул
    private $art_end;
    private $price_start;   //цена
    private $price_end;
    private $desc_start;    //описание
    private $desc_end;
    private $har_start;
    private $har_end;
    private $check;
    private $img_start;     //картинки
    private $img_end;
    private $search;    //Замена в урле для большой картинки
    private $replace;
    private $log_file;
    public $reg_exp_name;
    public $reg_exp_price;
    public $reg_exp_desc;
    public $reg_exp_har;
    public $reg_exp_img;
    public $reg_exp_art;
    public $reg_exp_charset;
    public $name;
    public $cat_name;
    public $art;
    public $price;
    public $desc;
    public $har;
    public $imgs;

    public function __construct(Options $options) {
        $this->siteUrl = $options->siteUrl;
        $this->name_start = $this->addSlashes(trim($options->name_start));
        $this->name_end = $this->addSlashes(trim($options->name_end));
        $this->art_start = $this->addSlashes(trim($options->art_start));
        $this->art_end = $this->addSlashes(trim($options->art_end));
        $this->price_start = $this->addSlashes(trim($options->price_start));
        $this->price_end = $this->addSlashes(trim($options->price_end));
        $this->desc_start = $this->addSlashes(trim($options->desc_start));
        $this->desc_end = $this->addSlashes(trim($options->desc_end));
        $this->har_start = $this->addSlashes(trim($options->har_start));
        $this->har_end = $this->addSlashes(trim($options->har_end));
        $this->check = $options->check;
        $this->img_start = $this->addSlashes(trim($options->img_start));
        $this->img_end = $this->addSlashes(trim($options->img_end));
        $this->search = $options->search;
        $this->replace = $options->replace;
        $this->log_file = 'Parser.txt';
        $this->name = array();
        $this->cat_name = array();
        $this->art = array();
        $this->price = array();
        $this->desc = array();
        $this->imgs = array();
    }

    #Экранирует спец символы для регулярок

    private function addSlashes($str) {
        $search = array(" ", "#", ".", "^", "(", ")", '"', "'", "-", "[", "]", "*", "+", "|", "?", "$", "{skip}");
        $replace = array("\s", "\#", "\.", "\^", "\(", "\)", '\"', "\'", "\-", "\[", "\]", "\*", "\+", "\|", "\?", "\$", ".*");
        return str_replace($search, $replace, $str);
    }  

    private function delete_dir($dir) {
        if (is_dir($dir)) {
            $d = opendir($dir);
            while ($f = readdir($d)) {
                if ($f != "." && $f != "..") {
                    unlink($dir . DIRECTORY_SEPARATOR . $f);
                }
            }
            closedir($d);
            rmdir($dir);
        }
    }

    public function parse() {
        $reg_exp_name = '#' . $this->name_start . '(.*)' . $this->name_end . '#Uis';
        $reg_exp_price = '#' . $this->price_start . '(.*)' . $this->price_end . '#Uis';
        $reg_exp_desc = '#' . $this->desc_start . '(.*)' . $this->desc_end . '#Uis';
        $reg_exp_har = '#' . $this->har_start . '(.*)' . $this->har_end . '#Uis';
        $reg_exp_img = '#' . $this->img_start . '(.*)' . $this->img_end . '#Uis';
        $reg_exp_art = '#' . $this->art_start . '(.*)' . $this->art_end . '#Uis';
        $urls = array();
        $file = fopen("result.csv", "w");
        fwrite($file, b"\xEF\xBB\xBF");
        fwrite($file, "Категория;Название;Артикул;Цена;Описание;Характеристики;картинка\r\n");
        fclose($file);
        $host = parse_url($this->siteUrl, PHP_URL_HOST);
        $site_dir = "tovar_img" . DIRECTORY_SEPARATOR . $host;
        if (is_dir($site_dir))
            $this->delete_dir($site_dir);
        @mkdir($site_dir); //создаём папку для картинок с названием как сайт
        $links = array();
        $links = explode("\r\n", file_get_contents("tovar_links.txt"));
        foreach ($links as $str) {
            $this->cat_name[] = end(explode(";", $str));
            $urls[] = reset(explode(";", $str));
        }
            $mc = new MyMultiCurl($this->siteUrl, $this->cat_name, $reg_exp_name, $reg_exp_art, $reg_exp_price, $reg_exp_desc, $reg_exp_har, $this->check, $reg_exp_img);
            foreach ($urls as $url) {
                $mc->addUrl($url);
            }
            $mc->wait();
    }
    
}

?>
